package com.softqin.washer.utils;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 

* @ClassName: UiUtils 

* @Description: TODO(UI������) 

* @author ��ʢΰ

* @date 2014-12-22 ����10:39:39
 */
public final class UiUtils
{
	private final static String TAG = "UiUtils";
	
    /** ȫ�ֵļ��ؿ�����Ѿ���ɳ�ʼ��. */
    private static ProgressDialog mProgressDialog;

    private static final List<Context> mDialogs = new ArrayList<Context>();

    /** 
     * ���ʿ�� Listener 
     *  
     * @author Lei 
     */
    // ��Ϊ���಻��activity����ͨ��̳нӿڵķ�����ȡ��������¼�   
    public interface OnClickYesListener
    {
        abstract void onClickYes();
    }

    /** 
     * ���ʿ�� Listener 
     *  
     */
    public interface OnClickNoListener
    {
        abstract void onClickNo();
    }

    public static void removeProgressDialog()
    {
        if (null != mProgressDialog && mProgressDialog.isShowing())
        {
            mProgressDialog.dismiss();
        }
    }

    public static void showProgressDialog(Context context, String title,
            String text, final OnClickYesListener listenerYes,
            CharSequence yesText, final OnClickNoListener listenerNo,
            CharSequence noText, boolean cancelable)
    {
        if (null == mProgressDialog)
        {
            mProgressDialog = new ProgressDialog(context);
        }
        mProgressDialog.setTitle(title);
        mProgressDialog.setMessage(text);

        if (null != listenerYes)
        {
            // ����ȷ����ť���̶��÷�����һ����ť�����setPositiveButton   
            mProgressDialog.setButton(DialogInterface.BUTTON_POSITIVE, yesText,
                    new OnClickListener()
                    {

                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            // ���ȷ�������   
                            if (listenerYes != null)
                            {
                                listenerYes.onClickYes();
                            }

                        }
                    });
        }

        if (null != listenerNo)
        {
            // ����ȡ��ť���̶��÷������ڶ�����ťҪ��setNegativeButton   
            mProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, noText,
                    new OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            // ���ȷ�������   
                            if (listenerNo != null)
                            {
                                listenerNo.onClickNo();
                            }

                        }
                    });
        }
        mProgressDialog.setCancelable(cancelable);
        mProgressDialog.show();
    }

    public static void showQuestionDialog(Context context, String title,
            String text, final OnClickYesListener listenerYes,
            final OnClickNoListener listenerNo)
    {
        Builder builder = new AlertDialog.Builder(context);

        if (!isBlank(text))
        {
            // �˷���Ϊdialogд����   
            final TextView textView = new TextView(context);
            textView.setText(text);
            LinearLayout layout = new LinearLayout(context);
            layout.setPadding(10, 0, 10, 0);
            layout.addView(textView, new LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
            builder.setView(layout);
        }
        // ����title   
        builder.setTitle(title);
        // ����ȷ����ť���̶��÷�����һ����ť�����setPositiveButton   
        builder.setPositiveButton("��", new OnClickListener()
        {
            public void onClick(DialogInterface dialog, int which)
            {
                // ���ȷ�������   
                if (listenerYes != null)
                {
                    listenerYes.onClickYes();
                }
            }
        });
        // ����ȡ��ť���̶��÷������ڶ�����ťҪ��setNegativeButton   
        builder.setNegativeButton("��", new OnClickListener()
        {
            public void onClick(DialogInterface dialog, int which)
            {
                // ���ȡ����   
                if (listenerNo != null)
                {
                    listenerNo.onClickNo();
                }
            }
        });

        // �������dialog�ɲ����԰����ؼ�trueΪ���ԣ�falseΪ������   
        builder.setCancelable(false);
        // ��ʾdialog   
        builder.create().show();

    }

    /** 
     * �����ַ�ķ��� 
     *  
     * @param str 
     * @return 
     */
    public static boolean isBlank(String str)
    {
        int strLen;
        if (str == null || (strLen = str.length()) == 0)
        {
            return true;
        }
        for (int i = 0; i < strLen; i++)
        {
            if ((Character.isWhitespace(str.charAt(i)) == false))
            {
                return false;
            }
        }
        return true;
    }

    public static int dip2px(Context context, float dipValue)
    {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    public static int px2dip(Context context, float pxValue)
    {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
    
	/**
	 * 
	 */
	public static Bitmap getTextBitMap(String text,int textSize) {
		Paint paint = new Paint(); 
		paint.setTextSize(textSize); 
		paint.setColor(Color.WHITE); 
		Bitmap bmp = Bitmap.createBitmap((int)paint.measureText(text),(int)paint.measureText(text), 
				                          Bitmap.Config.ARGB_4444); 
		Canvas canvasTemp = new Canvas(bmp); 
		canvasTemp.drawColor(Color.TRANSPARENT); 
		String fontName ="����"; 
		Typeface typeface = Typeface.create(fontName,Typeface.NORMAL);
		paint.setTypeface(typeface); 
		canvasTemp.drawText(text,(bmp.getWidth()-paint.measureText(text))/2,
				                 (bmp.getHeight()-paint.getTextSize()/2-10),paint);//10Ϊ��� 
        return bmp;
    }
}
